package com.buss.base.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.buss.base.model.DicData;
import com.buss.base.service.DicDataService;
import com.buss.core.controller.BaseController;
import com.buss.core.exception.ServiceException;
import com.buss.core.exception.WebException;
import com.buss.core.persistence.OrderBy;
import com.buss.core.persistence.Pagination;
import com.buss.core.persistence.ResultList;
import com.buss.core.sysparam.SysStaticParam;

@Controller
@RequestMapping(value = "/admin/dicdata")
public class DicDataController extends BaseController {

	@Autowired
	private DicDataService dicDataService;
	
	/**
	 * 数据字典首页
	 */
	@Override
	@RequestMapping("/page")
	public ModelAndView listPage(@RequestParam(required=false) String menuid) {
		ModelAndView view = new ModelAndView("dicdata/listpage");
		view.addObject(SysStaticParam._MENUID_NAME, menuid);
		return view;
	}

	@Override
	/**
	 * 新增、修改页面
	 */
	@RequestMapping("/{type}")
	public ModelAndView editPage(@PathVariable String type, Long id) {
		ModelAndView view = new ModelAndView("dicdata/edit");
		DicData dicData = new DicData();
		DicData parentDicData = null;
		if(EDIT_PAGE.equals(type)){
			dicData = dicDataService.loadDicData(id);
			if(dicData.getParentId() != 0){
				parentDicData = dicDataService.loadDicData(dicData.getParentId());
			}
		}else{
			if(id != 0){
				parentDicData = dicDataService.loadDicData(id);
			}
		}
		if(parentDicData == null){
			parentDicData = new DicData();
			parentDicData.setId(0L);
			parentDicData.setName("数据字典");
		}
		view.addObject("pDicData",parentDicData);
		view.addObject("dicData", dicData);
		return view;
	}

	@RequestMapping("/extaddpage")
	public ModelAndView editPage(String code,HttpServletRequest request) {
		ModelAndView view = new ModelAndView("dicdata/edit");
		DicData dicData = dicDataService.loadDicData(getCompanyId(request), code);
		if(dicData == null){
			dicData = new DicData();
			dicData.setId(0L);
			dicData.setName("数据字典");
		}
		view.addObject("pDicData",dicData);
		view.addObject("dicData", new DicData());
		return view;
	}
	
	@RequestMapping("/delete")
	@ResponseBody
	public Map<String, Object> deleteDicData(Long[] ids) throws WebException {
		try {
			dicDataService.removeDicData(ids);
		} catch (ServiceException e) {
			throw new WebException(e.getMessage());
		}
		return SysStaticParam.resultMessage("删除数据字典成功!");
	}
	
	/**
	 * 新增数据字典信息
	 * @param dicData
	 * @param request
	 * @return
	 * @throws WebException
	 */
	@RequestMapping("/add")
	@ResponseBody
	public Map<String,Object> add( DicData dicData,HttpServletRequest request)
		throws WebException{
		dicData.setCompanyId(getCompanyId(request));
		try {
			dicData = dicDataService.saveDicData(dicData);
		} catch (ServiceException e) {
			throw new WebException(e.getMessage());
		}
		Map<String, Object> map = SysStaticParam.resultMessage("新增数据字典成功!");
		map.put("dicData", dicData);
		return map;
	}

	/**
	 * 修改数据字典
	 * @param dicData
	 * @param request
	 * @return
	 */
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String,Object> edit( DicData dicData,HttpServletRequest request){
		dicData.setCompanyId(getCompanyId(request));
		dicDataService.editDicData(dicData);
		Map<String,Object> map = SysStaticParam.resultMessage("修改数据字典成功!");
		map.put("dicData", dicData);
		return map;
	}
	
	/**
	 * 根据上级ID获取数据字典列表
	 * @param parentId
	 * @param name
	 * @param request
	 * @return
	 */
	@RequestMapping("/list/{parentId}")
	@ResponseBody
	public Map<String,Object> list(@PathVariable Long parentId,String name, Pagination page,
			String orderField,String orderType, HttpServletRequest request){
		OrderBy orderBy = new OrderBy();
		orderBy.add(orderField, orderType);
		ResultList<DicData> resultList = dicDataService.queryDicData(getCompanyId(request), parentId, name, page,orderBy);
		Map<String,Object> map =  SysStaticParam.resultMessage("获取数据字典列表信息成功!");
		map.put(SysStaticParam._RESULT, resultList);
		return map;
	}
	
	/**
	 * 根据上级code获取数据字典信息
	 * @param parentCode
	 * @param page
	 * @param request
	 * @return
	 */
	@RequestMapping("/listbycode/{parentCode}")
	@ResponseBody
	public Map<String,Object> listByCode(@PathVariable String parentCode, Pagination page,
			String orderField,String orderType,HttpServletRequest request){
		OrderBy orderBy = new OrderBy();
		orderBy.add(orderField, orderType);
		ResultList<DicData> resultList = dicDataService.queryDicData(getCompanyId(request), parentCode, page,orderBy);
		Map<String,Object> map = SysStaticParam.resultMessage("获取数据字典列表信息成功!");
		map.put(SysStaticParam._RESULT, resultList);
		return map;
	}
	
	public Map<String, Object> delete(Long[] ids) {
		return null;
	}
}
